package com.lanhao.aot.carrierintegration.br1express;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;

import cn.hutool.http.HttpRequest;

public class BR1ExpressUtil {
	private static final Logger LOG = LoggerFactory.getLogger(BR1ExpressUtil.class);
	private static final String BASE_URL = "https://wms.br1express.net";
	private static final String API_KEY = "34b97b88-8e2f-4f41-b123-dc9c1532df0a";
	private static final String CUSTOMER_ID = "60598";
	private static final String USER_NAME = "ESPACE";
//	private static final String SHIPPING_CODE = "PAC-TZ";
	private static final String SHIPPING_CODE = "PAC-TZ-1";
	
	private static final String CREATE_URL = BASE_URL + "/api-json/pms/createorder.aspx?Key=" + API_KEY;
	private static final String PRINT_URL = BASE_URL + "/api-json/pms/getlabel.aspx?Key=" + API_KEY;

	// https://wms.br1express.net//api-json/GetShippingList.aspx?Key=34b97b88-8e2f-4f41-b123-dc9c1532df0a
	public static void main(String[] args) {
//		createOrder();

		printOrder("P24022109324900001");
		
		// {"result":"success","code":"100","No":"P24013011490500008","serviceCode":null,"totalRate":"34.850","message":"","TrackingNo":"QQ578165964BR","items":[{"TrackingNo":"QQ578165964BR","description":null,"amount":null}]}
	}

	public static void createOrder() {
		JSONObject request = new JSONObject();

		request.put("customer_ID", CUSTOMER_ID);
		request.put("status", "30");
		request.put("from_PersonName", "Lily");
		request.put("from_CountryCode", "BR");
		request.put("from_CompanyName", "");
		request.put("from_AddressLine1", "RuaJoaoCabriotiMendes");
		request.put("from_AddressLine2", ""); // 区
		request.put("from_AddressLine3", "-");
		request.put("from_PostalCode", "83055320");
		request.put("from_Zip4", "");
		request.put("from_City", "SaoJosedoRioPreto");
		request.put("from_StateProvinceCode", "PR");
		request.put("from_PhoneNumber", "17991293629");
		request.put("from_TaxNo", "");
		
		request.put("to_PersonName", "KunKun");
		request.put("to_CountryCode", "BR");
		request.put("to_CompanyName", "");
		request.put("to_AddressLine1", "Avenida Rui Barbosa,2529,Armazém 14");
		request.put("to_AddressLine2", "Bairro Ipê");
		request.put("to_AddressLine3", "-");
		request.put("to_PostalCode", "83055320");
		request.put("to_Zip4", "");
		request.put("to_City", "São José dos Pinhais");
		request.put("to_StateProvinceCode", "PR");
		request.put("to_PhoneNumber", "41999329486");
		request.put("to_TaxNo", "");
		request.put("orderNo", "20240806001994");
		request.put("serviceCode", SHIPPING_CODE);
		request.put("remark", "");
		request.put("Distribution", "");
		request.put("Source", "");

		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		
		item.put("Unit", "KG");
		item.put("weight", 0.01);
		item.put("length", 1);
		item.put("width", 1);
		item.put("height", 1);
		item.put("SKU", "SKU");
		item.put("Quantity", 1);

		items.add(item);
		request.put("items", items);

		String requestBody = request.toJSONString();
		LOG.info("请求BR1Express请求下单url={}，request={}", CREATE_URL, requestBody);
		String respBody = HttpRequest.post(CREATE_URL).timeout(20000).body(requestBody).execute().body();
		LOG.info("请求BR1Express下单结束，得到响应值respBody={}", respBody);
		if(StringUtils.isNotBlank(respBody)) {
			JSONObject respObject = JSON.parseObject(respBody);
			String code = respObject.getString("code");
			String message = respObject.getString("message");
			if("100".equals(code)) {
				String no = respObject.getString("No");
				String trackingNo = respObject.getString("TrackingNo");
				String printRespBody = printOrder(no);
				if(StringUtils.isNotBlank(printRespBody)) {
					JSONObject printRespObject = JSON.parseObject(printRespBody);
					String printCode = printRespObject.getString("code");
					if("100".equals(printCode)) {
					}
				}
			} else {
				
			}
		}
		// 请求BR1Express下单结束，得到响应值respBody={"result":"success","code":"100","No":"P24013011490500008","serviceCode":null,"totalRate":"34.850","message":"","TrackingNo":"QQ578165964BR","items":[{"TrackingNo":"QQ578165964BR","description":null,"amount":null}]}
		// {"result":"failure","code":"101","No":null,"serviceCode":null,"totalRate":null,"message":"预报失败,错误提示:预报服务商失败：Número do Logradouro de Origem não informado.","TrackingNo":null,"items":null}

	}

	public static String printOrder(String ID) {
		JSONObject requestBody = new JSONObject();
		requestBody.put("customer_ID", CUSTOMER_ID);
		requestBody.put("ID", ID);
		LOG.info("请求BR1Express打印订单url={}，request={}", PRINT_URL, requestBody);
		String respBody = HttpRequest.get(PRINT_URL).timeout(15000).body(requestBody.toJSONString()).execute().body();
		LOG.info("请求BR1Express打印订单结束，得到响应值respBody={}", respBody);
		// {"success":"failure","Code":"103","message":"失败!查询单号不存在","Enmessage":"fails"}
		// {"result":"success","code":"100","message":"成功","ID":"33db4479fed640d49cefd80dac807d11","No":"P24013011490500008","OrderNo":"20210806001999","TrackingNo":"QQ578165964BR","label":"https://wms.br1express.net/temp/57e80449-cffa-4f3d-b509-ff5e6ebfe02f.pdf"}
		return respBody;
	}
}
